<link rel="stylesheet" type="text/css" href="../stylesheet/style.css"/>
<?php
/*
* @author Nguyen Xuan Hong
* @copyright 2011
*/

require_once('../includes/footballaward.tbl.php');
require_once('../includes/footballclub.tbl.php');
require_once('../includes/footballmatch.tbl.php');

$id = (int)sget('id');
$award_id = (int)sget('award');
if($mode == 'listaward' || $mode == 'editaward')
{
	$pClsFootballAward = new clsFootballAwardTable();
	
	if($mode == 'listaward')
	{
		if($pClsFootballAward->avaibleSubmit())
		{
			$pClsFootballAward->loadBuffer();
			$pClsFootballAward->insertDB();
			echo acpredirect("Đã thêm Giải đấu");
			exit();
		}
		echo $pClsFootballAward->createForm('Thêm Giải đấu');
	}
	else if($mode == 'editaward')
	{
		if($pClsFootballAward->avaibleSubmit())
		{
			$pClsFootballAward->loadBuffer();
			$pClsFootballAward->updateDB('id',$id);
			echo acpredirect("Đã cập nhật Giải đấu", 'page='.$page.'&mode=listaward');
			exit();
		}
		$pClsFootballAward->loadDB('id',$id);
		echo $pClsFootballAward->createForm("Sửa thông tin Giải đấu '".$pClsFootballAward->value('name')."'");
	}
	
	createHeaderForm('Danh sách Giải đấu');
	$list_football_award = $pClsFootballAward->listobj();
	
	require_once('../includes/templates.php');
	$table = new clsTable('lstaward',array("STT","Tên Giải đấu","DS Đội bóng","DS Trận đấu","Sửa","Xóa"));
	$i = 0;
	if($list_football_award)
	{
		foreach($list_football_award as $award)
		{
			$i++;
			$html_club = '<a href="?page='.$page.'&award='.$award->value('id').'&mode=listclub">DS Đội</a>';
			$html_match = '<a href="?page='.$page.'&award='.$award->value('id').'&mode=listmatch">DS Đấu</a>';	
			$html_edit = '<a href="?page='.$page.'&mode=editaward&id='.$award->value('id').'">Sửa</a>';	
			$html_delete = '<a href="?page='.$page.'&mode=deleteaward&id='.$award->value('id').'">Xóa</a>';
			$table->addRow($i, array($i, $award->value('name'), $html_club, $html_match, $html_edit, $html_delete));
		}
	}
	echo $table->putHTML(true);
	exit();
}

if($_GET['mode'] == 'deleteaward')
{		
	// Redefine insert
	$pClsFootballAward = new clsFootballAwardTable($id);
	if($_POST['submit'])
	{
		$pClsFootballAward->removeDB('id', $id);
		echo acpredirect('Đã xoá Giải đấu', 'page='.$page.'&mode=listaward');
		exit();
	}
	else if($_POST['nosubmit'])
	{
		echo acpredirect('Trở về', 'page='.$page.'&mode=listaward');
		exit();
	}
	
	createDeleteForm("Giải đấu '".$pClsFootballAward->value('name')."'");   
	exit();
}
//------------------------------------------------------------------------------------------

if($mode == 'listclub' || $mode == 'editclub')
{
	$pClsFootballClub = new clsFootballClubTable();
	
	if($mode == 'listclub')
	{
		if($pClsFootballClub->avaibleSubmit())
		{
			$pClsFootballClub->loadBuffer();
			$pClsFootballClub->insertDB();
			echo acpredirect("Đã thêm Đội bóng");
			exit();
		}
		echo $pClsFootballClub->createForm('Thêm Đội bóng');
	}
	else if($mode == 'editclub')
	{
		if($pClsFootballClub->avaibleSubmit())
		{
			$pClsFootballClub->loadBuffer();
			$pClsFootballClub->updateDB('id',$id);
			echo acpredirect("Đã cập nhật Đội bóng", 'page='.$page.'&award='.$award_id.'&mode=listclub');
			exit();
		}
		$pClsFootballClub->loadDB('id',$id);
		echo $pClsFootballClub->createForm("Sửa thông tin Đội bóng '".$pClsFootballClub->value('name')."'");
	}
	
	$pClsFootballAward = new clsFootballAwardTable($award_id);
	createHeaderForm('Danh sách Đội bóng giải '.$pClsFootballAward->value('name'));
	$list_football_club = $pClsFootballClub->listobj($award_id, 100);
	
	require_once('../includes/templates.php');
	$table = new clsTable('lstclub',array("STT","Mã đội","Tên Đội bóng","Sửa","Xóa"));
	$i = 0;
	if($list_football_club)
	{
		foreach($list_football_club as $club)
		{
			$i++;
			$html_edit = '<a href="?page='.$page.'&award='.$award_id.'&mode=editclub&id='.$club->value('id').'">Sửa</a>';	
			$html_delete = '<a href="?page='.$page.'&award='.$award_id.'&mode=deleteclub&id='.$club->value('id').'">Xóa</a>';
			$table->addRow($i, array($i, $club->value('code'), $club->value('name'), $html_edit, $html_delete));
		}
	}
	echo $table->putHTML(true);
	exit();
}

if($_GET['mode'] == 'deleteclub')
{		
	// Redefine insert
	$pClsFootballClub = new clsFootballClubTable($id);
	if($_POST['submit'])
	{
		$pClsFootballClub->removeDB('id', $id);
		echo acpredirect('Đã xoá Đội bóng', 'page='.$page.'&award='.$award_id.'&mode=listclub');
		exit();
	}
	else if($_POST['nosubmit'])
	{
		echo acpredirect('Trở về', 'page='.$page.'&award='.$award_id.'&mode=listclub');
		exit();
	}
	
	createDeleteForm("Đội bóng '".$pClsFootballClub->value('name')."'");   
	exit();
}

//------------------------------------------------------------------------------------------

if($mode == 'listmatch' || $mode == 'editmatch')
{
	$pClsFootballMatch = new clsFootballMatchTable();
	$pClsFootballClub = new clsFootballClubTable();
    $pClsFootballMatch->attList['team1_id']['list'] = $pClsFootballClub->cbolist($award_id);
	$pClsFootballMatch->attList['team2_id']['list'] = $pClsFootballClub->cbolist($award_id);
	
	if($mode == 'listmatch')
	{
		if($pClsFootballMatch->avaibleSubmit())
		{
			$pClsFootballMatch->loadBuffer();
			$pClsFootballMatch->insertDB();
			echo acpredirect("Đã thêm Trận đấu");
			exit();
		}
		echo $pClsFootballMatch->createForm('Thêm Trận đấu');
	}
	else if($mode == 'editmatch')
	{
		if($pClsFootballMatch->avaibleSubmit())
		{
			$pClsFootballMatch->loadBuffer();
			$pClsFootballMatch->updateDB('id',$id);
			echo acpredirect("Đã cập nhật Trận đấu", 'page='.$page.'&award='.$award_id.'&mode=listmatch');
			exit();
		}
		$pClsFootballMatch->loadDB('id',$id);
		echo $pClsFootballMatch->createForm("Sửa thông tin Trận đấu '".$pClsFootballMatch->value('name')."'");
	}
	
	$pClsFootballAward = new clsFootballAwardTable($award_id);
	createHeaderForm('Danh sách Trận đấu giải '.$pClsFootballAward->value('name'));
	$list_football_match = $pClsFootballMatch->listobj($award_id, 100);
	
	require_once('../includes/templates.php');
	$table = new clsTable('lstmatch',array("STT","Mã trận","Tên Trận đấu","Đội bóng","Sửa","Xóa"));
	$i = 0;
	if($list_football_match)
	{
		foreach($list_football_match as $match)
		{
			$i++;
			$html_edit = '<a href="?page='.$page.'&award='.$award_id.'&mode=editmatch&id='.$match->value('id').'">Sửa</a>';	
			$html_delete = '<a href="?page='.$page.'&award='.$award_id.'&mode=deletematch&id='.$match->value('id').'">Xóa</a>';
			
			$pClsFootballClub->load($match->value('team1_id'));
			$team1 = $pClsFootballClub->value('name');
			$pClsFootballClub->load($match->value('team2_id'));
			$team2 = $pClsFootballClub->value('name');
			$table->addRow($i, array($i, $match->value('code'), $match->value('name'),$team1.'-'.$team2, $html_edit, $html_delete));
		}
	}
	echo $table->putHTML(true);
	exit();
}

if($_GET['mode'] == 'deletematch')
{		
	// Redefine insert
	$pClsFootballMatch = new clsFootballMatchTable($id);
	if($_POST['submit'])
	{
		$pClsFootballMatch->removeDB('id', $id);
		echo acpredirect('Đã xoá Đội bóng', 'page='.$page.'&award='.$award_id.'&mode=listmatch');
		exit();
	}
	else if($_POST['nosubmit'])
	{
		echo acpredirect('Trở về', 'page='.$page.'&award='.$award_id.'&mode=listmatch');
		exit();
	}
	
	$pClsFootballClub = new clsFootballClubTable();
	$pClsFootballClub->load($pClsFootballMatch->value('team1_id'));
	$team1 = $pClsFootballClub->value('name');
	$pClsFootballClub->load($pClsFootballMatch->value('team2_id'));
	$team2 = $pClsFootballClub->value('name');
		
	createDeleteForm("Đội bóng '".$pClsFootballMatch->value('name').": ".$team1.'-'.$team2."'");   
	exit();
}

